package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.GoodsMapper;
import cn.scxy.bookcity.pojo.entity.HighSearch;
import cn.scxy.bookcity.pojo.entity.Seratch;
import cn.scxy.bookcity.pojo.vo.*;
import cn.scxy.bookcity.service.IGoodsService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Data
@Service
@Slf4j
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<GoodsListItemVO> list() {
        List<GoodsListItemVO> list = goodsMapper.list();
        for (GoodsListItemVO goodsListItemVO : list) {
            System.out.println("---------"+goodsListItemVO);
        }
        return list;
    }

    /**
     * 只查询订单表列表的几个
     * @return
     * 张正荣
     */
    @Override
    public List<GoodsListItemVO> listByNum(int num) {
        List<GoodsListItemVO> list = goodsMapper.listByNum(num);
        for (GoodsListItemVO goodsListItemVO : list) {
            System.out.println("---------"+goodsListItemVO);
        }
        return list;
    }

    /**
     * 分组分3页查询
     * @return
     * 冯江龙
     */
    @Override
    public List<GoodsNameAndUrlVO> listLimit3() {
        List<GoodsNameAndUrlVO> goodsNameAndUrlVOS = goodsMapper.listLimit3();
        if (goodsNameAndUrlVOS==null){
            String message = "查询失败,查询的商品不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return goodsNameAndUrlVOS;
    }

    @Override
    public GoodsStandardVO getStandardById(Long id) {
        GoodsStandardVO standardById = goodsMapper.getStandardById(id);
        if (standardById==null){
            String message = "查询失败,查询的商品不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardById;
    }
    /**
     * 只查询推荐商品列表的几个
     * @return
     * 张正荣
     */
    @Override
    public List<GoodsListItemVO> selectGoodsListBySort() {
        List<GoodsListItemVO> list = goodsMapper.selectGoodsListBySort();

        return list;
    }

    /**
     * 搜索框搜索结果
     * @param
     * @return
     */
    @Override
    public List<GoodsSearchListVO> search(Seratch seratch) {
        List<GoodsSearchListVO> search = goodsMapper.search(seratch);
//        if(search.getGoodsName()==null || search.getGoodsName().isEmpty()){
//            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"请输入商品名称");
//        }
//        if(search.getState()!=1){
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品未上架");
//        }
        return search;
    }

    /**
     * 高级搜索
     * @return
     */
    @Override
    public List<GoodsHighSearchListVO> highSearch(HighSearch hs) {
        List<GoodsHighSearchListVO> highSearch = goodsMapper.highSearch(hs);
        return highSearch;
    }

    @Override
    public List<GoodsSearchListVO> searchhot(Long hotid) {
       HotSearchListVO hot= goodsMapper.selectHotById(hotid);
        System.out.println("hot = " + hot.getName());
      List<GoodsSearchListVO> list= goodsMapper.selectSerchByHotName(hot.getName());
      if(list==null){
          throw new ServiceException(ServiceCode.ERR_SELECT,"搜索失败");
      }
        return list;
    }

    @Override
    public List<GoodsSearchListVO> loadTypeId(Long id) {
        List<GoodsSearchListVO> list= goodsMapper.loadTypeId(id);
        return list;
    }

    /**
     * 搜索页面展示
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchList() {
        List<GoodsSearchListVO> searchList = goodsMapper.searchList();
        return searchList;
    }

    /**
     * 通过id展示列表
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchListById() {
        List<GoodsSearchListVO> listById = goodsMapper.searchListById();
        return listById;
    }

    /**
     * 通过销量展示列表
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchListByNums() {
        List<GoodsSearchListVO> listByNums = goodsMapper.searchListByNums();
        return listByNums;
    }

    /**
     * 通过出版时间展示列表
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchListByHouseTime() {
        List<GoodsSearchListVO> listByHouseTime = goodsMapper.searchListByHouseTime();
        return listByHouseTime;
    }

    /**
     * 通过价格展示列表
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchListByMoney() {
        List<GoodsSearchListVO> listByMoney = goodsMapper.searchListByMoney();
        return listByMoney;
    }

    /**
     * 通过评论数量展示列表
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchListByComments() {
        List<GoodsSearchListVO> listByComments = goodsMapper.searchListByComments();
        return listByComments;
    }

    /**
     * 通过类别Id查询
     * @param categoryId
     * @return
     */
    @Override
    public List<GoodsSearchListVO> searchListByCategoryId(Integer categoryId) {
        List<GoodsSearchListVO> listBycategoryId = goodsMapper.searchListByCategoryId(categoryId);
        return listBycategoryId;
    }


}
